package com.shrct.businessprocess.customerservice.impl;

import com.shrct.businessprocess.customerservice.RCNonProjectService;
import com.shrct.businessprocess.customerservice.RCNonProjectServiceInnerStatus;
import com.shrct.businessprocess.customerservice.RCNonProjectServiceStatus;
import com.shrct.businessprocess.customerservice.RCServiceRecord;
import com.shrct.businessprocess.customerservice.impl.RCServiceRecordImpl;
import com.shrct.businessprocess.customerservice.proxy.RCNonProjectServiceProxy;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.product.RCBrand;
import com.shrct.businessprocess.product.RCProductType;
import com.shrct.businessprocess.product.impl.RCBrandImpl;
import com.shrct.businessprocess.product.impl.RCProductTypeImpl;
import edu.fudan.langlab.domain.statemachine.ModelObjectLogService;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCNONPROJECTSERVICE_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcnonprojectservice")
public class RCNonProjectServiceImpl extends BaseModelObject implements RCNonProjectService {
  public RCNonProjectServiceImpl() {
    super();
  }
  
  private RCNonProjectServiceInnerStatus InnerStatus;
  
  public RCNonProjectServiceInnerStatus getInnerStatus() {
    return this.InnerStatus;
  }
  
  private ModelObjectLogService getModelObjectLogService() {
    return getBeanFactory().getBean(ModelObjectLogService.class);
  }
  
  private void _start() {
    
    this.status = RCNonProjectServiceStatus.created;
    InnerStatus=RCNonProjectServiceInnerStatus.created;
  }
  
  public void start() {
    if(InnerStatus==null){
    	_start();
    	getModelObjectLogService().createLog(this,"InnerStatus","Start");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCNonProjectServiceEventsManager.class).fireStarted(this);					
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException("allready started");
    }
  }
  
  private void _finish(final Date finishDate, final Collection<RCEmployee> workers, final String finishComment) {
    
    this.workers.clear();
    this.workers.addAll(workers);
    this.finishDate = finishDate;
    this.finishComment = finishComment;
    this.status = RCNonProjectServiceStatus.finished;
    InnerStatus=RCNonProjectServiceInnerStatus.finished;
  }
  
  public void finish(final Date finishDate, final Collection<RCEmployee> workers, final String finishComment) {
    if(InnerStatus==RCNonProjectServiceInnerStatus.created){
    	_finish(finishDate,workers,finishComment);
    	getModelObjectLogService().createLog(this,"InnerStatus","finish");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCNonProjectServiceEventsManager.class).fireFinish(this,finishDate,workers,finishComment);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(InnerStatus.name());
    }
  }
  
  private void _cancel() {
    
    this.status = RCNonProjectServiceStatus.canceled;
    InnerStatus=RCNonProjectServiceInnerStatus.canceled;
  }
  
  public void cancel() {
    if(InnerStatus==RCNonProjectServiceInnerStatus.created){
    	_cancel();
    	getModelObjectLogService().createLog(this,"InnerStatus","cancel");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCNonProjectServiceEventsManager.class).fireCancel(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(InnerStatus.name());
    }
  }
  
  private void _delete() {
    getObjectFactory().delete(this);
    
  }
  
  public void delete() {
    if(InnerStatus==RCNonProjectServiceInnerStatus.created){
    	_delete();
    	getModelObjectLogService().createLog(this,"InnerStatus","delete");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCNonProjectServiceEventsManager.class).fireDelete(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(InnerStatus.name());
    }
  }
  
  private void _update(final Procedure1<? super RCNonProjectService> updator) {
    
    updator.apply(this);
  }
  
  public void update(final Procedure1<? super RCNonProjectService> updator) {
    if(InnerStatus==RCNonProjectServiceInnerStatus.created){
    	_update(updator);
    	getModelObjectLogService().createLog(this,"InnerStatus","update");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCNonProjectServiceEventsManager.class).fireUpdate(this,updator);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(InnerStatus.name());
    }
  }
  
  private void _notifyManager() {
    
    this.status = RCNonProjectServiceStatus.notified;
    InnerStatus=RCNonProjectServiceInnerStatus.notified;
  }
  
  public void notifyManager() {
    if(InnerStatus==RCNonProjectServiceInnerStatus.created||InnerStatus==RCNonProjectServiceInnerStatus.notified){
    	_notifyManager();
    	getModelObjectLogService().createLog(this,"InnerStatus","notifyManager");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCNonProjectServiceEventsManager.class).fireNotifyManager(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(InnerStatus.name());
    }
  }
  
  public Boolean isCreated() {
    return com.shrct.businessprocess.customerservice.RCNonProjectServiceInnerStatus.created==InnerStatus;
  }
  
  public Boolean isFinished() {
    return com.shrct.businessprocess.customerservice.RCNonProjectServiceInnerStatus.finished==InnerStatus;
  }
  
  public Boolean isNotified() {
    return com.shrct.businessprocess.customerservice.RCNonProjectServiceInnerStatus.notified==InnerStatus;
  }
  
  public Boolean isCanceled() {
    return com.shrct.businessprocess.customerservice.RCNonProjectServiceInnerStatus.canceled==InnerStatus;
  }
  
  private Date applyDate;
  
  public Date getApplyDate() {
    return this.applyDate;
  }
  
  public RCNonProjectService setApplyDate(final Date applyDate) {
    this.applyDate = applyDate;
    return this;			
    
  }
  
  private String customerName;
  
  public String getCustomerName() {
    return this.customerName;
  }
  
  public RCNonProjectService setCustomerName(final String customerName) {
    this.customerName = customerName;
    return this;			
    
  }
  
  private String customerLinkPerson;
  
  public String getCustomerLinkPerson() {
    return this.customerLinkPerson;
  }
  
  public RCNonProjectService setCustomerLinkPerson(final String customerLinkPerson) {
    this.customerLinkPerson = customerLinkPerson;
    return this;			
    
  }
  
  private String customerLinkMail;
  
  public String getCustomerLinkMail() {
    return this.customerLinkMail;
  }
  
  public RCNonProjectService setCustomerLinkMail(final String customerLinkMail) {
    this.customerLinkMail = customerLinkMail;
    return this;			
    
  }
  
  private String customerLinkPhone;
  
  public String getCustomerLinkPhone() {
    return this.customerLinkPhone;
  }
  
  public RCNonProjectService setCustomerLinkPhone(final String customerLinkPhone) {
    this.customerLinkPhone = customerLinkPhone;
    return this;			
    
  }
  
  private String customerLinkAddress;
  
  public String getCustomerLinkAddress() {
    return this.customerLinkAddress;
  }
  
  public RCNonProjectService setCustomerLinkAddress(final String customerLinkAddress) {
    this.customerLinkAddress = customerLinkAddress;
    return this;			
    
  }
  
  private Double serviceDays;
  
  public Double getServiceDays() {
    return this.serviceDays;
  }
  
  public RCNonProjectService setServiceDays(final Double serviceDays) {
    this.serviceDays = serviceDays;
    return this;			
    
  }
  
  private Integer serviceWorkers;
  
  public Integer getServiceWorkers() {
    return this.serviceWorkers;
  }
  
  public RCNonProjectService setServiceWorkers(final Integer serviceWorkers) {
    this.serviceWorkers = serviceWorkers;
    return this;			
    
  }
  
  @ManyToMany(targetEntity = RCBrandImpl.class)
  @JoinTable(name = "m2m_rcnonprojectservice_stddevicesupplier", joinColumns = @JoinColumn(name = "rcnonprojectservice_id") , inverseJoinColumns = @JoinColumn(name = "rCBrand_inv_id") )
  private Collection<RCBrand> stdDeviceSupplier = new java.util.ArrayList<com.shrct.businessprocess.product.RCBrand>();;
  
  public Collection<RCBrand> getStdDeviceSupplier() {
    return this.stdDeviceSupplier;
  }
  
  public RCNonProjectService setStdDeviceSupplier(final Iterable<RCBrand> stdDeviceSupplier) {
    removeAllStdDeviceSupplier();
    for(RCBrand ca : stdDeviceSupplier){
    	addtoStdDeviceSupplier(ca);
    }
    return this;		
    
  }
  
  public RCNonProjectService addtoStdDeviceSupplier(final RCBrand rCBrand) {
    this.stdDeviceSupplier.add(rCBrand);
    return this;
    
  }
  
  public RCNonProjectService removeAllStdDeviceSupplier() {
    this.stdDeviceSupplier.clear();
    return this;	
    
  }
  
  public RCNonProjectService removeFromStdDeviceSupplier(final RCBrand rCBrand) {
    this.stdDeviceSupplier.remove(rCBrand);
    return this;
    
  }
  
  private String deviceSupplier;
  
  public String getDeviceSupplier() {
    return this.deviceSupplier;
  }
  
  public RCNonProjectService setDeviceSupplier(final String deviceSupplier) {
    this.deviceSupplier = deviceSupplier;
    return this;			
    
  }
  
  @ManyToMany(targetEntity = RCProductTypeImpl.class)
  @JoinTable(name = "m2m_rcnonprojectservice_stddevicetype", joinColumns = @JoinColumn(name = "rcnonprojectservice_id") , inverseJoinColumns = @JoinColumn(name = "rCProductType_inv_id") )
  private Collection<RCProductType> stdDeviceType = new java.util.ArrayList<com.shrct.businessprocess.product.RCProductType>();;
  
  public Collection<RCProductType> getStdDeviceType() {
    return this.stdDeviceType;
  }
  
  public RCNonProjectService setStdDeviceType(final Iterable<RCProductType> stdDeviceType) {
    removeAllStdDeviceType();
    for(RCProductType ca : stdDeviceType){
    	addtoStdDeviceType(ca);
    }
    return this;		
    
  }
  
  public RCNonProjectService addtoStdDeviceType(final RCProductType rCProductType) {
    this.stdDeviceType.add(rCProductType);
    return this;
    
  }
  
  public RCNonProjectService removeAllStdDeviceType() {
    this.stdDeviceType.clear();
    return this;	
    
  }
  
  public RCNonProjectService removeFromStdDeviceType(final RCProductType rCProductType) {
    this.stdDeviceType.remove(rCProductType);
    return this;
    
  }
  
  private String deviceType;
  
  public String getDeviceType() {
    return this.deviceType;
  }
  
  public RCNonProjectService setDeviceType(final String deviceType) {
    this.deviceType = deviceType;
    return this;			
    
  }
  
  @JoinColumn(name = "sales_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee sales;
  
  public RCEmployee getSales() {
    return this.sales;
  }
  
  public RCNonProjectService setSales(final RCEmployee sales) {
    this.sales = sales;
    return this;			
    
  }
  
  @ManyToMany(targetEntity = RCServiceRecordImpl.class)
  @JoinTable(name = "m2m_rcnonprojectservice_servicerecords", joinColumns = @JoinColumn(name = "rcnonprojectservice_id") , inverseJoinColumns = @JoinColumn(name = "rCServiceRecord_inv_id") )
  private Collection<RCServiceRecord> serviceRecords = new java.util.ArrayList<com.shrct.businessprocess.customerservice.RCServiceRecord>();;
  
  public Collection<RCServiceRecord> getServiceRecords() {
    return this.serviceRecords;
  }
  
  public RCNonProjectService setServiceRecords(final Iterable<RCServiceRecord> serviceRecords) {
    removeAllServiceRecords();
    for(RCServiceRecord ca : serviceRecords){
    	addtoServiceRecords(ca);
    }
    return this;		
    
  }
  
  public RCNonProjectService addtoServiceRecords(final RCServiceRecord rCServiceRecord) {
    this.serviceRecords.add(rCServiceRecord);
    return this;
    
  }
  
  public RCNonProjectService removeAllServiceRecords() {
    this.serviceRecords.clear();
    return this;	
    
  }
  
  public RCNonProjectService removeFromServiceRecords(final RCServiceRecord rCServiceRecord) {
    this.serviceRecords.remove(rCServiceRecord);
    return this;
    
  }
  
  private Date finishDate;
  
  public Date getFinishDate() {
    return this.finishDate;
  }
  
  public RCNonProjectService setFinishDate(final Date finishDate) {
    this.finishDate = finishDate;
    return this;			
    
  }
  
  @ManyToMany(targetEntity = RCEmployeeImpl.class)
  @JoinTable(name = "m2m_rcnonprojectservice_workers", joinColumns = @JoinColumn(name = "rcnonprojectservice_id") , inverseJoinColumns = @JoinColumn(name = "rCEmployee_inv_id") )
  private Collection<RCEmployee> workers = new java.util.ArrayList<com.shrct.businessprocess.organization.RCEmployee>();;
  
  public Collection<RCEmployee> getWorkers() {
    return this.workers;
  }
  
  public RCNonProjectService setWorkers(final Iterable<RCEmployee> workers) {
    removeAllWorkers();
    for(RCEmployee ca : workers){
    	addtoWorkers(ca);
    }
    return this;		
    
  }
  
  public RCNonProjectService addtoWorkers(final RCEmployee rCEmployee) {
    this.workers.add(rCEmployee);
    return this;
    
  }
  
  public RCNonProjectService removeAllWorkers() {
    this.workers.clear();
    return this;	
    
  }
  
  public RCNonProjectService removeFromWorkers(final RCEmployee rCEmployee) {
    this.workers.remove(rCEmployee);
    return this;
    
  }
  
  private String finishComment;
  
  public String getFinishComment() {
    return this.finishComment;
  }
  
  public RCNonProjectService setFinishComment(final String finishComment) {
    this.finishComment = finishComment;
    return this;			
    
  }
  
  private RCNonProjectServiceStatus status;
  
  public RCNonProjectServiceStatus getStatus() {
    return this.status;
  }
  
  public RCNonProjectService setStatus(final RCNonProjectServiceStatus status) {
    this.status = status;
    return this;			
    
  }
  
  public void doAddtoServiceRecords(final RCServiceRecord serviceRecord) {
    boolean _contains = this.serviceRecords.contains(serviceRecord);
    boolean _not = (!_contains);
    if (_not) {
      this.addtoServiceRecords(serviceRecord);
    }
  }
  
  public static RCNonProjectService create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.customerservice.RCNonProjectService rCNonProjectService = new com.shrct.businessprocess.customerservice.impl.RCNonProjectServiceImpl(
    );
    objectFactory.create(rCNonProjectService);
    return rCNonProjectService;			
    
  }
  
  public static RCNonProjectService createRCNonProjectServiceImpl(final IObjectFactory objectFactory, final Procedure1<RCNonProjectService> init) {
    com.shrct.businessprocess.customerservice.RCNonProjectService rCNonProjectService = new com.shrct.businessprocess.customerservice.impl.RCNonProjectServiceImpl(
    );
    objectFactory.create(rCNonProjectService);
    init.apply(rCNonProjectService);
    
    return rCNonProjectService;			
    
  }
  
  public RCNonProjectServiceProxy toProxy() {
    com.shrct.businessprocess.customerservice.proxy.RCNonProjectServiceProxy proxy = new com.shrct.businessprocess.customerservice.proxy.RCNonProjectServiceProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    proxy.setInnerStatus(this.getInnerStatus().name());
    return proxy;
    
  }
}
